using System;
using System.Collections.Generic;
using System.Text;
using GameFramework;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;
using GameEngine;

namespace Projet_X0
{
    public class ProjectileMesh : IMesh
    {
        private Texture projectileTexture = null;
        private Sprite projectileSprite = null;
        
        private float height = 17;
        private float width = 17;

        public ProjectileMesh()
        {
        }

        public override void Render()
        {
            Position projPos = (Position)owner.GetOwner().GetState((int)StatesID.Position);
            Orientation orientation = (Orientation)owner.GetOwner().GetState((int)StatesID.Orientation);
            Point position = new Point(Display.HRES / 2 + (int)projPos.GetPos().GetX() - (int)GameManager.GetServices().display.GetCameraPosition().GetX(), Display.VRES / 2 + (int)projPos.GetPos().GetY() - (int)GameManager.GetServices().display.GetCameraPosition().GetY());
            double b = Math.Atan2(position.Y, position.X);
            double r = Math.Sqrt(position.X * position.X + position.Y * position.Y);
            float angle = orientation.Get();
            Point pos = new Point((int)(r * Math.Cos(b - angle)) + position.X - (int)width / 2, (int)(r * Math.Sin(b - angle)) + position.Y - (int)height / 2);

            projectileSprite.Begin(SpriteFlags.AlphaBlend);
            projectileSprite.Draw2D(projectileTexture, position, angle, pos, Color.White.ToArgb());
            projectileSprite.End();
        }

        public override void Terminate()
        {
            projectileSprite.Dispose();
            projectileTexture.Dispose();
        }

        public override void Init()
        {
            string src = ((Persistance)GameManager.GetServices().persistance).LoadMesh((int)EntityID.Projectile);

            projectileTexture = TextureLoader.FromFile(Display.grfDevice, src, 0, 0, 0, Usage.None, Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, System.Drawing.Color.FromArgb(0, 67, 171).ToArgb());
            projectileSprite = new Sprite(Display.grfDevice);            
        }

        public float GetHeight()
        {
            return height;
        }

        public float GetWidth()
        {
            return width;
        }
    }
}
